﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    public static PlayerController con;
    private void Awake()
    {
        con = this;
    }

    [Header("动画播放器")]
    private Animator an;
    private AnimatorStateInfo animsta;

    public float speed;
    [Header("往上的力")]
    public float force;
    
    private const string Idle = "Idle";
    private const string Jump = "Jump";
    private const string Attack1 = "Attack1";
    private const string Attack2 = "Attack2";
    private const string Attack3 = "Attack3";

    private int HitCount ;

    public Transform Lpoint;
    public Transform Rpoint;

    public CapsuleCollider2D sword;
    // Use this for initialization
    void Start () {
        an = gameObject.GetComponent<Animator>();
        HitCount = 0;
    }

    /// <summary>
    /// 连续攻击的实现
    /// </summary>
    void attack()
    {
        if (HitCount == 0 && animsta.normalizedTime > 0f)
        {
            an.SetTrigger("attack1");
            HitCount = 1;
        }
        else if (animsta.IsName(Attack1) && HitCount ==1 && animsta.normalizedTime > 0f)
        {
            an.SetTrigger("attack2");
            HitCount = 2;
        }
        else if (animsta.IsName(Attack2) && HitCount == 2 && animsta.normalizedTime > 0f)
        {
            an.SetTrigger("attack3");
            HitCount = 0;
        }
    }
    
    /// <summary>
    /// 玩家受到伤害
    /// </summary>
    public void PlayerHurt()
    {
        an.SetTrigger("IsHit");
    }
    
    // Update is called once per frame
    void Update () {
        sword.enabled = false;
        animsta = an.GetCurrentAnimatorStateInfo(0);
        if (J)
        {
            attack();
        }
        
        if (animsta.IsName(Idle))
        {
            HitCount = 0;
        }
        if (A && !animsta.IsName(Attack1) && !animsta.IsName(Attack2) && !animsta.IsName(Attack3))
        {
            transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
            transform.localScale = new Vector3(-2, 2, 1);
            an.SetInteger("State", 1);
        }
        
        else if (D && !animsta.IsName(Attack1) && !animsta.IsName(Attack2) && !animsta.IsName(Attack3))
        {
            transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
            transform.localScale = new Vector3(2, 2, 1);
            an.SetInteger("State", 1);
        }
        else
        {
            an.SetInteger("State", 0);
        }

        if (K && !animsta.IsName(Jump) && !animsta.IsName(Attack1) && !animsta.IsName(Attack2) && !animsta.IsName(Attack3))
        {
            an.SetTrigger("IsJump");
            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, force));
        }
    }

    bool A = false;
    public void PlayerLeft()
    {
        A = true;
    }
    public void NoPlayerLeft()
    {
        A = false;
    }

    bool D = false;
    public void PlayerRight()
    {
        D = true;
    }
    public void NoPlayerRight()
    {
        D = false;
    }

    bool J = false;
    public void PlayerAttack()
    {
        J = true;
    }
    public void NoPlayerAttack()
    {
        J = false;
    }

    bool K = false;
    public void PlayerJump()
    {
        K = true;
    }
    public void NoPlayerJump()
    {
        K = false;
    }


    public void AttackEnemy()
    {
        sword.enabled = true;
    }
}
